/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.matezk.dms.things.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.matezk.dms.things.enums.FotaScopeEnum;
import cn.matezk.dms.things.mapper.DeviceMapper;
import cn.matezk.dms.things.mapper.FotaPkgMapper;
import cn.matezk.dms.things.mapper.FotaTaskDetailMapper;
import cn.matezk.dms.things.mapper.FotaTaskMapper;
import cn.matezk.dms.things.model.entity.FotaPkgDO;
import cn.matezk.dms.things.model.entity.FotaTaskDO;
import cn.matezk.dms.things.model.entity.FotaTaskDetailDO;
import cn.matezk.dms.things.model.query.FotaTaskQuery;
import cn.matezk.dms.things.model.req.FotaPkgReq;
import cn.matezk.dms.things.model.req.FotaTaskReq;
import cn.matezk.dms.things.model.resp.FotaPkgResp;
import cn.matezk.dms.things.model.resp.FotaTaskDetailResp;
import cn.matezk.dms.things.model.resp.FotaTaskResp;
import cn.matezk.dms.things.service.FotaTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * FOTA任务
 *
 * @author 程道林
 * @since 2025/09/25 20:33
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FotaTaskServiceImpl extends BaseServiceImpl<FotaTaskMapper, FotaTaskDO, FotaTaskResp, FotaTaskDetailResp, FotaTaskQuery, FotaTaskReq> implements FotaTaskService {
    private FotaPkgMapper fotaPkgMapper;
    private FotaTaskDetailMapper fotaTaskDetailMapper;
    private DeviceMapper deviceMapper;

    @Override
    @Transactional
    public Long add(FotaTaskReq req) {
        Long taskId = super.add(req);
        List<FotaTaskDetailDO> details = new ArrayList<>();
        if (FotaScopeEnum.ALL.getValue().equals(req.getScope()))
        {
            FotaPkgDO fotaPkg = fotaPkgMapper.selectById(req.getPkgId());
            // 根据版本号查询待升级设备
            List<String> devices = deviceMapper.selectByProductVersion(fotaPkg.getProductId(), req.getOldVersion());
            req.setDeviceList(devices);
        }

        if (ObjectUtil.isNotEmpty(req.getDeviceList()))
        {
            // 指定了待升级设备
            for (String deviceName : req.getDeviceList())
            {
                FotaTaskDetailDO detail = new FotaTaskDetailDO();
                detail.setTaskId(taskId);
                detail.setDeviceName(deviceName);
                detail.setCreateTime(new Date());
                details.add(detail);
            }
        } else {

        }
        if (ObjectUtil.isEmpty(details))
        {
            throw new BusinessException("无待升级的设备");
        }
        fotaTaskDetailMapper.insertBatch(details);
        return taskId;
    }
}